home *** CD-ROM | disk | FTP | other *** search
/ Developer Helper 1: Phil & Dave's Excellent CD / Excellent CD HFS.raw / Moof / Goodies / HyperCard Goodies / HyperCard Dev. ToolKit / XCMD.Sources / XCmdGlue.inc < prev    next >
Text File  |  1987-08-06  |  12KB  |  446 lines

  1. { XCmdGlue.inc -- Sample glue routines to call back to HyperCard.  
  2.   See example use in Peek.p and Flash.p
  3.   By Dan Winkler.  DO NOT call the author!  Contact Apple Developer 
  4.   Support on AppleLink "MacDTS" or on MCI "MacTech".
  5.  
  6.       ©Apple Computer, Inc. 1987
  7.     All Rights Reserved.
  8. }
  9.  
  10. { The Pascal code for the XCMD or XFCN should include HyperXCmd.p at
  11.   the beginning in the USES clause and this file at the end with the $I
  12.   directive.  There must be a variable named "paramPtr" that is the argument
  13.   that was passed into the XCMD or XFCN. All strings are Pascal strings 
  14.   unless noted as zero-terminated strings (no length byte and the string 
  15.   goes until a zero byte is encountered). }
  16.  
  17.  
  18. PROCEDURE DoJsr(addr: ProcPtr); INLINE $205F,$4E90;
  19. { Jump subroutine to a procedure.   Pop address into A0, JSR (A0) }
  20.  
  21.  
  22. PROCEDURE SendCardMessage(msg: Str255);
  23. {  Send a HyperCard message (a command with arguments) to the current card. }
  24. BEGIN
  25.   WITH paramPtr^ DO
  26.     BEGIN
  27.       inArgs[1] := ORD(@msg);
  28.       request := xreqSendCardMessage;
  29.       DoJsr(entryPoint);
  30.     END;
  31. END;
  32.  
  33.  
  34. FUNCTION EvalExpr(expr: Str255): Handle;
  35. {  Evaluate a HyperCard expression and return the answer.  The answer is
  36.    a handle to a zero-terminated string. }
  37. BEGIN
  38.   WITH paramPtr^ DO
  39.     BEGIN
  40.       inArgs[1] := ORD(@expr);
  41.       request := xreqEvalExpr;
  42.       DoJsr(entryPoint);
  43.       EvalExpr := Handle(outArgs[1]);
  44.     END;
  45. END;
  46.  
  47.  
  48. FUNCTION StringLength(strPtr: Ptr): LongInt;
  49. {  Count the characters from where strPtr points until the next zero byte. 
  50.    Does not count the zero itself.  strPtr must be a zero-terminated string.  }
  51. BEGIN
  52.   WITH paramPtr^ DO
  53.     BEGIN
  54.       inArgs[1] := ORD(strPtr);
  55.       request := xreqStringLength;
  56.       DoJsr(entryPoint);
  57.       StringLength := outArgs[1];
  58.     END;
  59. END;
  60.  
  61.  
  62. FUNCTION StringMatch(pattern: Str255; target: Ptr): Ptr;
  63. { Perform case-insensitive match looking for pattern anywhere in
  64.   target, returning a pointer to first character of the first match,
  65.   in target or NIL if no match found.  pattern is a Pascal string,
  66.   and target is a zero-terminated string. }
  67. BEGIN
  68.   WITH paramPtr^ DO
  69.     BEGIN
  70.       inArgs[1] := ORD(@pattern);
  71.       inArgs[2] := ORD(target);
  72.       request := xreqStringMatch;
  73.       DoJsr(entryPoint);
  74.       StringMatch := Ptr(outArgs[1]);
  75.     END;
  76. END;
  77.  
  78.  
  79. PROCEDURE SendHCMessage(msg: Str255);
  80. {  Send a HyperCard message (a command with arguments) to HyperCard. }
  81. BEGIN
  82.   WITH paramPtr^ DO
  83.     BEGIN
  84.       inArgs[1] := ORD(@msg);
  85.       request := xreqSendHCMessage;
  86.       DoJsr(entryPoint);
  87.     END;
  88. END;
  89.  
  90.  
  91. PROCEDURE ZeroBytes(dstPtr: Ptr; longCount: LongInt);
  92. {  Write zeros into memory starting at dstPtr and going for longCount 
  93.    number of bytes. }
  94. BEGIN
  95.   WITH paramPtr^ DO
  96.     BEGIN
  97.       inArgs[1] := ORD(dstPtr);
  98.       inArgs[2] := longCount;
  99.       request := xreqZeroBytes;
  100.       DoJsr(entryPoint);
  101.     END;
  102. END;
  103.  
  104.  
  105. FUNCTION PasToZero(str: Str255): Handle;
  106. {  Convert a Pascal string to a zero-terminated string.  Returns a handle
  107.    to a new zero-terminated string.  The caller must dispose the handle. }
  108. BEGIN
  109.   WITH paramPtr^ DO
  110.     BEGIN
  111.       inArgs[1] := ORD(@str);
  112.       request := xreqPasToZero;
  113.       DoJsr(entryPoint);
  114.       PasToZero := Handle(outArgs[1]);
  115.     END;
  116. END;
  117.  
  118.  
  119. PROCEDURE ZeroToPas(zeroStr: Ptr; VAR pasStr: Str255);
  120. {  Fill the Pascal string with the contents of the zero-terminated
  121.    string.  You create the Pascal string and pass it in as a VAR 
  122.    parameter.  Useful for converting the arguments of any XCMD to 
  123.    Pascal strings.}
  124. BEGIN
  125.   WITH paramPtr^ DO
  126.     BEGIN
  127.       inArgs[1] := ORD(zeroStr);
  128.       inArgs[2] := ORD(@pasStr);
  129.       request := xreqZeroToPas;
  130.       DoJsr(entryPoint);
  131.     END;
  132. END;
  133.  
  134.  
  135. FUNCTION StrToLong(str: Str31): LongInt;
  136. {  Convert a string of ASCII decimal digits to an unsigned long integer. }
  137. BEGIN
  138.   WITH paramPtr^ DO
  139.     BEGIN
  140.       inArgs[1] := ORD(@str);
  141.       request := xreqStrToLong;
  142.       DoJsr(entryPoint);
  143.       StrToLong := outArgs[1];
  144.     END;
  145. END;
  146.  
  147.  
  148. FUNCTION StrToNum(str: Str31): LongInt;
  149. {  Convert a string of ASCII decimal digits to a signed long integer.
  150.    Negative sign is allowed.  }
  151. BEGIN
  152.   WITH paramPtr^ DO
  153.     BEGIN
  154.       inArgs[1] := ORD(@str);
  155.       request := xreqStrToNum;
  156.       DoJsr(entryPoint);
  157.       StrToNum := outArgs[1];
  158.     END;
  159. END;
  160.  
  161.  
  162. FUNCTION StrToBool(str: Str31): BOOLEAN;
  163. {  Convert the Pascal strings 'true' and 'false' to booleans. }
  164. BEGIN
  165.   WITH paramPtr^ DO
  166.     BEGIN
  167.       inArgs[1] := ORD(@str);
  168.       request := xreqStrToBool;
  169.       DoJsr(entryPoint);
  170.       StrToBool := BOOLEAN(outArgs[1]);
  171.     END;
  172. END;
  173.  
  174.  
  175. FUNCTION StrToExt(str: Str31): Extended;
  176. {  Convert a string of ASCII decimal digits to an extended long integer. }
  177. VAR x: Extended;
  178. BEGIN
  179.   WITH paramPtr^ DO
  180.     BEGIN
  181.       inArgs[1] := ORD(@str);
  182.       inArgs[2] := ORD(@x);
  183.       request := xreqStrToExt;
  184.       DoJsr(entryPoint);
  185.       StrToExt := x;
  186.     END;
  187. END;
  188.  
  189.  
  190. FUNCTION LongToStr(posNum: LongInt): Str31;
  191. {  Convert an unsigned long integer to a Pascal string.  }
  192. VAR str: Str31;
  193. BEGIN
  194.   WITH paramPtr^ DO
  195.     BEGIN
  196.       inArgs[1] := posNum;
  197.       inArgs[2] := ORD(@str);
  198.       request := xreqLongToStr;
  199.       DoJsr(entryPoint);
  200.       LongToStr := str;
  201.     END;
  202. END;
  203.  
  204.  
  205. FUNCTION NumToStr(num: LongInt): Str31;
  206. {  Convert a signed long integer to a Pascal string.  }
  207. VAR str: Str31;
  208. BEGIN
  209.   WITH paramPtr^ DO
  210.     BEGIN
  211.       inArgs[1] := num;
  212.       inArgs[2] := ORD(@str);
  213.       request := xreqNumToStr;
  214.       DoJsr(entryPoint);
  215.       NumToStr := str;
  216.     END;
  217. END;
  218.  
  219.  
  220. FUNCTION NumToHex(num: LongInt; nDigits: INTEGER): Str31;
  221. {  Convert an unsigned long integer to a hexadecimal number and put it
  222.    into a Pascal string.  }
  223. VAR str: Str31;
  224. BEGIN
  225.   WITH paramPtr^ DO
  226.     BEGIN
  227.       inArgs[1] := num;
  228.       inArgs[2] := nDigits;
  229.       inArgs[3] := ORD(@str);
  230.       request := xreqNumToHex;
  231.       DoJsr(entryPoint);
  232.       NumToHex := str;
  233.     END;
  234. END;
  235.  
  236.  
  237. FUNCTION BoolToStr(bool: BOOLEAN): Str31;
  238. {  Convert a boolean to 'true' or 'false'.  }
  239. VAR str: Str31;
  240. BEGIN
  241.   WITH paramPtr^ DO
  242.     BEGIN
  243.       inArgs[1] := LongInt(bool);
  244.       inArgs[2] := ORD(@str);
  245.       request := xreqBoolToStr;
  246.       DoJsr(entryPoint);
  247.       BoolToStr := str;
  248.     END;
  249. END;
  250.  
  251.  
  252. FUNCTION ExtToStr(num: Extended): Str31;
  253. {  Convert an extended long integer to decimal digits in a string.  }
  254. VAR str: Str31;
  255. BEGIN
  256.   WITH paramPtr^ DO
  257.     BEGIN
  258.       inArgs[1] := ORD(@num);
  259.       inArgs[2] := ORD(@str);
  260.       request := xreqExtToStr;
  261.       DoJsr(entryPoint);
  262.       ExtToStr := str;
  263.     END;
  264. END;
  265.  
  266.  
  267. FUNCTION GetGlobal(globName: Str255): Handle;
  268. {  Return a handle to a zero-terminated string containing the value of 
  269.    the specified HyperTalk global variable.  }
  270. BEGIN
  271.   WITH paramPtr^ DO
  272.     BEGIN
  273.       inArgs[1] := ORD(@globName);
  274.       request := xreqGetGlobal;
  275.       DoJsr(entryPoint);
  276.       GetGlobal := Handle(outArgs[1]);
  277.     END;
  278. END;
  279.  
  280.  
  281. PROCEDURE SetGlobal(globName: Str255; globValue: Handle);
  282. {  Set the value of the specified HyperTalk global variable to be
  283.    the zero-terminated string in globValue.  The contents of the 
  284.    Handle are copied, so you must still dispose it afterwards.  }
  285. BEGIN
  286.   WITH paramPtr^ DO
  287.     BEGIN
  288.       inArgs[1] := ORD(@globName);
  289.       inArgs[2] := ORD(globValue);
  290.       request := xreqSetGlobal;
  291.       DoJsr(entryPoint);
  292.     END;
  293. END;
  294.  
  295.  
  296. FUNCTION GetFieldByName(cardFieldFlag: BOOLEAN; fieldName: Str255): Handle;
  297. {  Return a handle to a zero-terminated string containing the value of 
  298.    field fieldName on the current card.  You must dispose the handle.  }
  299. BEGIN
  300.   WITH paramPtr^ DO
  301.     BEGIN
  302.       inArgs[1] := ORD(cardFieldFlag);
  303.       inArgs[2] := ORD(@fieldName);
  304.       request := xreqGetFieldByName;
  305.       DoJsr(entryPoint);
  306.       GetFieldByName := Handle(outArgs[1]);
  307.     END;
  308. END;
  309.  
  310.  
  311. FUNCTION GetFieldByNum(cardFieldFlag: BOOLEAN; fieldNum: INTEGER): Handle;
  312. {  Return a handle to a zero-terminated string containing the value of 
  313.    field fieldNum on the current card.  You must dispose the handle.  }
  314. BEGIN
  315.   WITH paramPtr^ DO
  316.     BEGIN
  317.       inArgs[1] := ORD(cardFieldFlag);
  318.       inArgs[2] := fieldNum;
  319.       request := xreqGetFieldByNum;
  320.       DoJsr(entryPoint);
  321.       GetFieldByNum := Handle(outArgs[1]);
  322.     END;
  323. END;
  324.  
  325.  
  326. FUNCTION GetFieldByID(cardFieldFlag: BOOLEAN; fieldID: INTEGER): Handle;
  327. {  Return a handle to a zero-terminated string containing the value of 
  328.    the field whise ID is fieldID.  You must dispose the handle.  }
  329. BEGIN
  330.   WITH paramPtr^ DO
  331.     BEGIN
  332.       inArgs[1] := ORD(cardFieldFlag);
  333.       inArgs[2] := fieldID;
  334.       request := xreqGetFieldByID;
  335.       DoJsr(entryPoint);
  336.       GetFieldByID := Handle(outArgs[1]);
  337.     END;
  338. END;
  339.  
  340.  
  341. PROCEDURE SetFieldByName(cardFieldFlag: BOOLEAN; fieldName: Str255; fieldVal: Handle);
  342. {  Set the value of field fieldName to be the zero-terminated string 
  343.    in fieldVal.  The contents of the Handle are copied, so you must 
  344.    still dispose it afterwards.  }
  345. BEGIN
  346.   WITH paramPtr^ DO
  347.     BEGIN
  348.       inArgs[1] := ORD(cardFieldFlag);
  349.       inArgs[2] := ORD(@fieldName);
  350.       inArgs[3] := ORD(fieldVal);
  351.       request := xreqSetFieldByName;
  352.       DoJsr(entryPoint);
  353.     END;
  354. END;
  355.  
  356.  
  357. PROCEDURE SetFieldByNum(cardFieldFlag: BOOLEAN; fieldNum: INTEGER; fieldVal: Handle);
  358. {  Set the value of field fieldNum to be the zero-terminated string 
  359.    in fieldVal.  The contents of the Handle are copied, so you must 
  360.    still dispose it afterwards.  }
  361. BEGIN
  362.   WITH paramPtr^ DO
  363.     BEGIN
  364.       inArgs[1] := ORD(cardFieldFlag);
  365.       inArgs[2] := fieldNum;
  366.       inArgs[3] := ORD(fieldVal);
  367.       request := xreqSetFieldByNum;
  368.       DoJsr(entryPoint);
  369.     END;
  370. END;
  371.  
  372.  
  373. PROCEDURE SetFieldByID(cardFieldFlag: BOOLEAN; fieldID: INTEGER; fieldVal: Handle);
  374. {  Set the value of the field whose ID is fieldID to be the zero-
  375.    terminated string in fieldVal.  The contents of the Handle are 
  376.    copied, so you must still dispose it afterwards.  }
  377. BEGIN
  378.   WITH paramPtr^ DO
  379.     BEGIN
  380.       inArgs[1] := ORD(cardFieldFlag);
  381.       inArgs[2] := fieldID;
  382.       inArgs[3] := ORD(fieldVal);
  383.       request := xreqSetFieldByID;
  384.       DoJsr(entryPoint);
  385.     END;
  386. END;
  387.  
  388.  
  389. FUNCTION StringEqual(str1,str2: Str255): BOOLEAN;
  390. {  Return true if the two strings have the same characters.  
  391.    Case insensitive compare of the strings.  }
  392. BEGIN
  393.   WITH paramPtr^ DO
  394.     BEGIN
  395.       inArgs[1] := ORD(@str1);
  396.       inArgs[2] := ORD(@str2);
  397.       request := xreqStringEqual;
  398.       DoJsr(entryPoint);
  399.       StringEqual := BOOLEAN(outArgs[1]);
  400.     END;
  401. END;
  402.  
  403.  
  404. PROCEDURE ReturnToPas(zeroStr: Ptr; VAR pasStr: Str255);
  405. {  zeroStr points into a zero-terminated string.  Collect the 
  406.    characters from there to the next carriage Return and return 
  407.    them in the Pascal string pasStr.  If a Return is not found, 
  408.    collect chars until the end of the string. }
  409. BEGIN
  410.   WITH paramPtr^ DO
  411.     BEGIN
  412.       inArgs[1] := ORD(zeroStr);
  413.       inArgs[2] := ORD(@pasStr);
  414.       request := xreqReturnToPas;
  415.       DoJsr(entryPoint);
  416.     END;
  417. END;
  418.  
  419.  
  420. PROCEDURE ScanToReturn(VAR scanPtr: Ptr);
  421. {  Move the pointer scanPtr along a zero-terminated 
  422.    string until it points at a Return character
  423.    or a zero byte.  }
  424. BEGIN
  425.   WITH paramPtr^ DO
  426.     BEGIN
  427.       inArgs[1] := ORD(@scanPtr);
  428.       request := xreqScanToReturn;
  429.       DoJsr(entryPoint);
  430.     END;
  431. END;
  432.  
  433.  
  434. PROCEDURE ScanToZero(VAR scanPtr: Ptr);
  435. {  Move the pointer scanPtr along a zero-terminated 
  436.    string until it points at a zero byte.  }
  437. BEGIN
  438.   WITH paramPtr^ DO
  439.     BEGIN
  440.       inArgs[1] := ORD(@scanPtr);
  441.       request := xreqScanToZero;
  442.       DoJsr(entryPoint);
  443.     END;
  444. END;
  445.  
  446.